package com.example.springboot_code_read.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;

import java.io.IOException;

public class ConditionTest {


    public static void main(String[] args) throws IOException {

        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

        SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();

        Resource[] resources = resourcePatternResolver.getResources("classpath*:com/example/springboot_code_read/**/*.class");

        Condition condition = new MyOnClassCondition();

        for (Resource resource : resources) {

            MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);

            if (condition.matches(null, metadataReader.getAnnotationMetadata())) {
                System.out.println(resource.getFilename() + " 匹配");
            } else {
                System.out.println(resource.getFilename() + " 不匹配");
            }

            System.out.println("===================");

        }

    }

}
